package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.IDishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author mr.meng
 * @since 2025-10-17
 */
@Service
public class DishServiceImpl implements IDishService {

    @Autowired
    private DishMapper dishMapper;
    /**
     * 分页查询接口
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {

        //1.调用分页插件PageHelper方法进行拦截
        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        //2.调用mapper接口查询数据列表
        Page<DishVO> page = dishMapper.queryList(dto);

        //3.封装分也相应结束
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());

        return pageResult;
    }

    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    /**
     * 新增菜品和对应的口味
     *
     * @param dto
     */
    @Override
    public void add(DishDTO dto) {
        //将dto对象复制成菜品的实体对象
        Dish dish = BeanUtil.toBean(dto,Dish.class);
        //设置更多属性的值
        dish.setStatus(StatusConstant.DISABLE);//新增菜品为停售状态
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        //新增菜品数据
        dishMapper.insert(dish);
        //新增菜品口味数据
        List<DishFlavor> flavors = dto.getFlavors();
        if(CollUtil.isNotEmpty(flavors)){
            flavors.forEach(x->x.setDishId(dish.getId()));
            dishFlavorMapper.insertBath(flavors);
        }

    }

    /**
     * 根据id查询菜品和对应的口味
     *
     * @param id
     * @return
     */
    @Override
    public DishVO finById(Long id) {
        //根据id查询菜品数据
        Dish dish = dishMapper.selectById(id);
        //根据id查询口味
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        //封装响应数据
        DishVO dishVO = BeanUtil.toBean(dish,DishVO.class);
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    /**
     * 修改菜品接口
     *
     * @param dto
     */
    @Override
    public void update(DishDTO dto) {
        //将dto对象复制为菜品表实体对象
        Dish dish = BeanUtil.toBean(dto,Dish.class);
        dish.setUpdateUser(BaseContext.getCurrentId());
        dish.setUpdateTime(LocalDateTime.now());
        //更新菜品数据
        dishMapper.update(dish);
        //删除裁判关联的口味
        dishFlavorMapper.deleteBatchByDishId(Arrays.asList(dto.getId()));
        //添加菜品关键新的口味数据
        List<DishFlavor> flavors = dto.getFlavors();
        if(CollUtil.isNotEmpty(flavors)){
            flavors.forEach(x->x.setDishId(dish.getId()));
            dishFlavorMapper.insertBath(flavors);
        }
    }

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @Override
    public Result delete(List<Long> ids) {
        //判断菜品是否是启售状态，如果是不与删除
        for(Long id:ids){
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus()==StatusConstant.ENABLE){
                return Result.error(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断菜品是否被套餐关联
        Long count = setmealDishMapper.getByDishId(ids);
        if(count>0){
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //批量删除菜品数据
        dishMapper.deleteBatchById(ids);
        //批量删除菜品的口味数据
        dishFlavorMapper.deleteBatchByDishId(ids);

        return Result.success();
    }
}
